<template>
  <view
    class="content"
    :style="{
      'padding-top': height + 'px',
    }"
  >
    <view
      :style="{
        height: statusBarHeight + 'px',
      }"
    >
    </view>
    <view
      class="head"
      :style="{
        top: statusBarHeight + 'px',
      }"
    >
      <view class="head_left">
        <u-icon
          name="arrow-left"
          color="#444444"
          size="24"
          :bold="true"
          @click="toBack"
        ></u-icon>
      </view>
      <view class="text">意见反馈</view>
    </view>

    <view class="box">
      <view class="box_input">
        <view class="box_input_title">联系方式</view>
        <u--input
          placeholder="请填写手机号或邮箱"
          border="none"
          v-model="form.tel"
          placeholderStyle="font-size:32rpx;color:#888888;"
          prefixIconStyle="font-size: 32rpx;color: #888888"
          inputAlign="right"
        ></u--input>
      </view>
      <view class="box_title">反馈内容</view>
      <u--textarea
        v-model="form.textarea"
        placeholder="请描述你遇到的问题"
        border="bottom"
        placeholderClass="placeholderCss"
        :count="true"
        maxlength="500"
      ></u--textarea>

      <view class="box_title">附图</view>
      <view class="box_upload">
        <u-upload
          :fileList="fileList1"
          @afterRead="afterRead"
          @delete="deletePic"
          name="1"
          :maxCount="3"
        ></u-upload>
      </view>


      <view class="box_title">联系我们</view>
      <view class="box_guanfang">
        微信公众号：{{wechat}}<br>
        微信号：{{wechat_g}}
      </view>

      <view :style="{height:'200rpx'}"></view>

      <view class="bottom">
        <view class="bottom_btn" @click="subFn">提交反馈</view>
      </view>
    </view>

    <floatingBall></floatingBall>
  </view>
</template>
    
<script>
import selectType from "@/components/selectType.vue";
import config from "@/config"; // 配置文件
import storage from "@/common/storage.js"; // 缓存封装
export default {
  components: {
    selectType,
  },
  data() {
    return {
      statusBarHeight: 0,
      height: 0,
      form: {
        tel: "",
        textarea: "",
      },
      fileList1: [],
      file_id:[],
      wechat:"",
      wechat_g:"",
    };
  },
  onLoad(options) {
    this.phone = options.phone;
    this.form.tel = this.phone;
    this.getWeixinhao()
  },
  created() {
    const statusBarHeight = uni.getSystemInfoSync(); // 获取手机状态栏高度
    this.statusBarHeight = statusBarHeight.statusBarHeight;
    this.$nextTick(() => {
      uni
        .createSelectorQuery()
        .select(".head")
        .boundingClientRect((data) => {
          this.height = data.height;
        })
        .exec();
    });
  },
  methods: {

    getWeixinhao(){
      this.$http
        .appversionShanhai({})
        .then((res) => {
          console.log(res,'获取微信号');
          this.wechat = res.data.shanhai.wechat
          this.wechat_g = res.data.shanhai.wechat_g
          
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    },

    toBack() {
      uni.navigateBack({
        delta: 1,
      });
    },
    subFn() {
      // uni.$u.toast("提交成功，请等待我们的回复");

      if (this.form.tel == "") {
        uni.$u.toast("请输入联系方式");
        return;
      } else if (this.form.textarea == "") {
        uni.$u.toast("请输入反馈内容");
        return;
      }
      this.feedback();
    },
    feedback() {
      this.$http
        .helpFeedback({
          contact_information: this.form.tel,
          content: this.form.textarea,
          img:this.file_id.join(',')
        })
        .then((res) => {
          console.log(res);
          uni.$u.toast(res.message);
          setTimeout(() => {
            uni.navigateBack({
              delta: 1,
            });
          }, 1000);
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    },

    // 删除图片
    deletePic(event) {
      this[`fileList${event.name}`].splice(event.index, 1);
      this.file_id.splice(event.index, 1);
      console.log(this.file_id,'上传的id列表')
    },
    // 新增图片
    async afterRead(event) {
      // 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
      let lists = [].concat(event.file);
      let fileListLen = this[`fileList${event.name}`].length;
      lists.map((item) => {
        this[`fileList${event.name}`].push({
          ...item,
          status: "uploading",
          message: "上传中",
        });
      });
      for (let i = 0; i < lists.length; i++) {
        const result = await this.uploadFilePromise(lists[i].url);
        let item = this[`fileList${event.name}`][fileListLen];
        this[`fileList${event.name}`].splice(
          fileListLen,
          1,
          Object.assign(item, {
            status: "success",
            message: "",
            url: result,
          })
        );
        fileListLen++;
      }
    },
    uploadFilePromise(url) {
      let that = this;
      let token = storage.get("token");
      return new Promise((resolve, reject) => {
        let a = uni.uploadFile({
          url: config.uni_app_web_api_url + "/api/upload/image", // 仅为示例，非真实的接口地址
          filePath: url,
          name: "file",
          formData: {
            user: "test",
          },
          header: {
            "Access-Token": token,
          },
          success: (res) => {
            const data = JSON.parse(res.data);
            that.file_id.push(data.data.fileInfo.file_id);

            // console.log(data,'上传图片')
            console.log(that.fileList1,'上传的列表')
            console.log(that.file_id,'上传的id列表')
            resolve(res.data.data);
          },
        });
      });
    },
  },
};
</script>
    
    <style scoped lang="scss">
.content {
  box-sizing: border-box;
}
.head {
  width: 100%;
  padding: 18rpx 30rpx 47rpx;
  position: fixed;
  display: flex;
  justify-content: center;
  align-items: center;
  box-sizing: border-box;
  z-index: 99;
  background: #fff;

  .head_left {
    position: absolute;
    left: 30rpx;
  }

  .head_right {
    position: absolute;
    right: 30rpx;
    display: flex;
  }

  .text {
    font-size: 36rpx;
    font-family: PingFang SC-Bold, PingFang SC;
    font-weight: 400;
    color: #444444;
  }
}
.box {
  padding: 30rpx 0;
  box-sizing: border-box;
  .box_input {
    padding: 32rpx 30rpx;
    box-sizing: border-box;
    border-bottom: 1rpx solid #dadbde;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .box_input_title {
      font-size: 32rpx;
      color: #444444;
      margin-right: 150rpx;
    }
  }
  .box_title {
    padding: 0 30rpx;
    font-size: 32rpx;
    color: #444444;
    margin-top: 32rpx;
  }
  .box_guanfang{
    padding: 0 30rpx;
    font-size: 32rpx;
    color: #444444;
    margin-top: 32rpx;
  }
  .box_upload{
    padding: 0 30rpx;
    box-sizing: border-box;
    margin-top: 32rpx;
    border-bottom: 1rpx solid #dadbde;
    padding-bottom: 20rpx;
  }
  /deep/.u-textarea {
    padding: 0;
    .u-textarea__field {
      padding: 16rpx 30rpx;
    }
  }
  /deep/.uni-textarea-placeholder {
    font-size: 32rpx;
  }

  .bottom {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    margin-bottom: 80rpx;
    .bottom_btn {
      width: 630rpx;
      height: 96rpx;
      background: linear-gradient(360deg, #50b674 0%, #aeddbd 100%);
      border-radius: 48rpx;
      text-align: center;
      line-height: 96rpx;
      font-size: 30rpx;
      color: #ffffff;
      margin: 0 auto;
    }
  }
}
</style>